//
//  RecommendViewController.m
//  MedicalTip
//
//  Created by 徐潇 on 15/4/13.
//  Copyright (c) 2015年 徐潇. All rights reserved.
//

#import "RecommendViewController.h"

@interface RecommendViewController ()

@end

@implementation RecommendViewController

- (void)viewWillAppear:(BOOL)animated
{
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    
    NSString *url_string = [NSString stringWithFormat:@"http://api.yi18.net/cook/list?page=2&limit=10&type=id"];
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
       // NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        NSMutableDictionary *Dic = responseObject;
        self.foodArray = [NSMutableArray array];
        NSMutableArray *array = [Dic objectForKey:@"yi18"];
        for (NSDictionary *dic in array) {
            Food *food = [[Food alloc] init];
            food.foodId = [dic objectForKey:@"id"];
            food.name = [dic objectForKey:@"name"];
            food.tag = [dic objectForKey:@"tag"];
            food.img = [dic objectForKey:@"img"];
            food.count = [dic objectForKey:@"count"];
            [self.foodArray addObject:food];
            [self.myTableView reloadData];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];

    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.newsArray = [NSMutableArray array];
    Food *food1 = [[Food alloc] init];
    Food *food2 = [[Food alloc] init];
    Food *food3 = [[Food alloc] init];
    Food *food4 = [[Food alloc] init];
    Food *food5 = [[Food alloc] init];
    Food *food6 = [[Food alloc] init];

    [self.foodArray addObject:food1];
    [self.foodArray addObject:food2];
    [self.foodArray addObject:food3];
    [self.foodArray addObject:food4];
    [self.foodArray addObject:food5];
    [self.foodArray addObject:food6];
    self.view.backgroundColor = [UIColor yellowColor];
    
    self.myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 375, 140)];
    self.myScrollView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.myScrollView];
    [_myScrollView release];

    // 扩展视图的 宽/高 的用来显示图片数组
    self.myScrollView.contentSize = CGSizeMake(self.myScrollView.frame.size.width * self.foodArray.count, 0);
    self.button1 = [[UIButton alloc] initWithFrame:CGRectMake(15, 145, 65, 25)];
    self.button1.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.button1];
    [self.button1 release];
    self.button1.tag = 10001;
    [self.button1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.button2 = [[UIButton alloc] initWithFrame:CGRectMake(125, 145, 65, 25)];
    self.button2.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.button2];
    [self.button2 release];
    self.button2.tag = 10002;
    [self.button2 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.button3 = [[UIButton alloc] initWithFrame:CGRectMake(240, 145, 65, 25)];
    self.button3.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.button3];
    [self.button3 release];
    self.button3.tag = 10003;
    [self.button3 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.button4 = [[UIButton alloc] initWithFrame:CGRectMake(15, 175, 65, 25)];
    self.button4.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.button4];
    [self.button4 release];
    self.button4.tag = 10004;
    [self.button4 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.button5 = [[UIButton alloc] initWithFrame:CGRectMake(125, 175, 65, 25)];
    self.button5.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.button5];
    [self.button5 release];
    self.button5.tag = 10005;
    [self.button5 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.button6 = [[UIButton alloc] initWithFrame:CGRectMake(240, 175, 65, 25)];
    self.button6.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.button6];
    [self.button6 release];
    self.button6.tag = 10006;
    [self.button6 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 205, self.view.frame.size.width, self.view.frame.size.height - 200)];
    self.myTableView.backgroundColor = [UIColor whiteColor];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    [_myTableView release];

    [self addHeader];
    [self addFooter];
}

- (void)buttonAction:(UIButton *)button
{
    if (button.tag == 10001) {
        NSLog(@"button1");
        NewsListViewController *newsList = [[NewsListViewController alloc] init];
        [self.navigationController pushViewController:newsList animated:YES];
        newsList.URL = @"http://api.yi18.net/news/list?id=3&page=1&limit=20";
    }else if (button.tag == 10002){
        NSLog(@"button2");
    }else if (button.tag == 10003){
        NSLog(@"buttton3");
    }else if (button.tag == 10004){
        NSLog(@"buttton4");
    }else if (button.tag == 10005){
        NSLog(@"buttton5");
    }else if (button.tag == 10006){
        NSLog(@"buttton6");
    }
    
    
    
}
#pragma mark --下拉刷新
- (void)addHeader
{
    //    __unsafe_unretained typeof(self) vc = self;
    __block RecommendViewController *vc = self;
    
    // 添加下拉刷新头部控件
    [self.myTableView addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        
        NSLog(@"下拉刷新，开始网络请求第一页数据");
        
        
        //（为了模拟延迟加载数据，因此2秒后才让刷新请求结束，实际项目中可以在数据请求成功后，停止下拉效果）
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [vc.myTableView reloadData];
            // 结束刷新
            [vc.myTableView headerEndRefreshing];
        });
        
        
    }];
    
#pragma mark  --自动刷新(一进入程序就下拉刷新)
    [self.myTableView headerBeginRefreshing];
}

#pragma mark --上拉加载更多
- (void)addFooter
{
    //    __unsafe_unretained typeof(self) vc = self;
    __block RecommendViewController *vc = self;
    
    // 添加上拉刷新尾部控件
    [self.myTableView addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        NSLog(@"上拉加载更多，开始网络请求更多数据");
        
        // （模拟延迟加载数据，因此2秒后才调用）
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [vc.myTableView reloadData];
            // 结束刷新
            [vc.myTableView footerEndRefreshing];
        });
        
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{   NSLog(@"%ld", self.foodArray.count);
    return self.foodArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    NewsShowTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[NewsShowTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }
//    Food *food1 = [self.foodArray objectAtIndex:0];
//    NSLog(@"--9999--%@", food1.foodId);
//    NSLog(@"-----%@", food1.name);
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

- (void)layoutSubviews
{
    NSLog(@"===== %ld", self.foodArray.count);
    Food *food = [self.foodArray objectAtIndex:0];
    NSLog(@"--%@", food.foodId);
    NSLog(@"%@", food.name);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
